package edu.chl.jadetalk.business;

import edu.chl.jadetalk.core.*;
import edu.chl.jadetalk.db.ICommentFacade;
import edu.chl.jadetalk.db.IImageFacade;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 * Handles logic involed with image operations.
 * @author Erik
 */
@Stateless
public class ImageManagerBean implements ImageManagerBeanLocal {

    @EJB
    private IImageFacade imageFacade;
    
    @EJB
    private ICommentFacade commentFacade;

    @Override
    public void storeImage(Image image) {
        Logger.getAnonymousLogger().log(Level.INFO, "Storing image entry in database.");
        imageFacade.create(image);
    }

    @Override
    public List<Image> listImages() {
            return imageFacade.findAll();
    }
    
    @Override
    public List<Image> listImagesRange(int[] range) {
            return imageFacade.findRange(range);
    }
    
    @Override
    public Image getImage(long id){
        return imageFacade.find(id);
    }
    
    @Override
    public void createCommentOnImage(User user, String text, Image image){
        
        // Create empty comment thread
        CommentThread thread = new CommentThread();
        thread.setComments(new ArrayList<Comment>());
        
        // Create comment
        Comment comment = new Comment();
        comment.setCommentText(text);
        comment.setUser(user);
        comment.setRatings(new ArrayList<Rating>());
        comment.setCommentThread(thread);
        
        // Add this comment to image
        imageFacade.addComment(image, comment);
    }

    @Override
    public void createRatingOnImage(User user, Image image, double rating) {
        // Check rating
        if(rating < 0){
            rating = 0;
        }
        else if(rating > 5){
            rating = 5;
        }
        
        // TODO: Implement
    }
}
